package com.sanpu.kxe.business.repay.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.card.view.MyBankListActivity;
import com.sanpu.kxe.business.repay.bean.MccListOutput;
import com.sanpu.kxe.business.repay.bean.RefundPlanInput;
import com.sanpu.kxe.business.repay.bean.RefundPlanOutput;
import com.sanpu.kxe.business.repay.contract.RefundPlanContract;
import com.sanpu.kxe.business.repay.model.RepayModel;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.rxservice.RxError;
import com.sanpu.kxe.utils.IntentTool;
import com.sanpu.kxe.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：还款详情
 * Created by 薛勇军 on 2019/4/10.
 */

public class RefundPlanPresenter implements RefundPlanContract.Presenter {

    private RefundPlanContract.View mView;

    private CompositeSubscription mSubscription;

    private List<MccListOutput> mccList = new ArrayList<>();

    private List<String> selectList = new ArrayList<>();

    private RefundPlanInput planInput = null;

    public RefundPlanPresenter(RefundPlanContract.View mView, RefundPlanInput planInput) {
        this.mView = mView;
        this.planInput = planInput;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getMccList() {
        mView.showLoadingDialog(true);
        Subscription subscription = RepayModel.getInstance().getMccList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MccListOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<MccListOutput> mccListOutputs) {
                        if (mccListOutputs != null) {
                            mccList.addAll(mccListOutputs);
                            for (MccListOutput mcc:mccList) {
                                selectList.add(mcc.getMccName());
                            }
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void executePlan(String repayOrderNo, String wheelCode, List<RefundPlanOutput.RefundPlan> refundPlanList) {
        if (TextUtils.isEmpty(wheelCode) || !wheelCode.contains(" ")) {
            ToastUtil.show("请选择城市！");
            return;
        }
        if (planInput == null) return;
        String[] code = wheelCode.split(" ");
        mView.showLoadingDialog(false);
        Subscription subscription = RepayModel.getInstance()
                .executePlan(planInput.getApprtId(), repayOrderNo, code[0], code[1], refundPlanList)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.dismissLoadingDialog();
                        ToastUtil.show("执行计划成功！");
                        IntentTool.startActivity(mView.getContext(), MyBankListActivity.class, true);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void makePlan() {
        mView.showLoadingDialog(false);
        Subscription subscription = RepayModel.getInstance().createPlan(planInput)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RefundPlanOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        if (e instanceof RxError) {
                            RxError error = (RxError) e;
                            GlobalDialog dialog = new GlobalDialog(mView.getContext())
                                    .headerIcon(R.drawable.img_fail_result)
                                    .title("温馨提示")
                                    .content(error.getMessage());
                            dialog.show();
                        } else {
                            mView.showErrorText(e);
                        }
                    }

                    @Override
                    public void onNext(RefundPlanOutput refundPlanOutput) {
                        mView.showRefundPlanView(refundPlanOutput);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void updatePlanPaymentDays(String repayOrderNo, String expectedPaymentDays) {
        mView.showLoadingDialog(false);
        Subscription subscription = RepayModel.getInstance().updatePlanPaymentDays(repayOrderNo, expectedPaymentDays)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RefundPlanOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        if (e instanceof RxError) {
                            RxError error = (RxError) e;
                            GlobalDialog dialog = new GlobalDialog(mView.getContext())
                                    .headerIcon(R.drawable.img_fail_result)
                                    .title("温馨提示")
                                    .content(error.getMessage());
                            dialog.show();
                        } else {
                            mView.showErrorText(e);
                        }
                    }

                    @Override
                    public void onNext(RefundPlanOutput refundPlanOutput) {
                        mView.showRefundPlanView(refundPlanOutput);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public List<String> getSelectList() {
        return selectList;
    }

    @Override
    public String getMccCodeByName(String mccName) {
        if (!TextUtils.isEmpty(mccName) && mccList != null) {
            for (MccListOutput mcc : mccList) {
                if (TextUtils.equals(mccName, mcc.getMccName())) {
                    return mcc.getMccCode();
                }
            }
        }
        return "";
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
